<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>内容管理</el-breadcrumb-item>
			<el-breadcrumb-item>心理知识管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-form :inline="true">
			<el-form-item label="标题">
				<el-input v-model="queryData.title" clearable @clear="queryList" placeholder="请输入标题"></el-input>
			</el-form-item>
			<el-form-item label="简介">
				<el-input v-model="queryData.introduct" clearable @clear="queryList" placeholder="请输入简介"></el-input>
			</el-form-item>
			<el-form-item label="详情内容">
				<el-input v-model="queryData.context" clearable @clear="queryList" placeholder="请输入详情内容"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="queryList">搜索</el-button>
				<el-button type="primary" @click="add">新增</el-button>
				<el-button type="danger" @click="removeBatch">批量删除</el-button>
			</el-form-item>
		</el-form>
		<!-- 表格展示区域 -->
		<el-table ref="multipleTable" :data="dataList" border style="width:
100%" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55"></el-table-column>
			<!-- prop：该列要展示的数据的字段 lable：列标题 -->
			<el-table-column prop="id" label="ID" width="55"></el-table-column>
			<el-table-column prop="title" label="标题" width="120"></el-table-column>
			<el-table-column prop="introduct" label="简介"></el-table-column>
			<el-table-column prop="chart" label="封面图">
				<template slot-scope="scope">
					<el-image style="width: 100px; height: 100px;text-align:
			center;" :src="'http://localhost:8080/sfzy'+ scope.row.chart" fit="contain"></el-image>
				</template>
			</el-table-column>
			<el-table-column prop="context" label="详情内容" show-overflow-tooltip></el-table-column>
			<el-table-column prop="creatTime" label="时间" width="100"></el-table-column>
			<el-table-column prop="consultantName" label="咨询师" width="100"></el-table-column>
			<el-table-column label="操作" width="200">
				<!-- slot-scope 作用域插槽 -->
				<template slot-scope="scope">
					<el-link type="primary" @click="edit(scope.row)">编辑</el-link>
					<el-link type="primary" @click="remove(scope.row.id)">删除
					</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryData.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="queryData.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>
		<!-- 新增和修改的对话框 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<!-- :model="新增或修改需要提交的数据对象" -->
			<el-form :model="addOrUpdateData" ref="addOrEditForm" label-width="90px">
				<el-form-item label="标题">
					<el-input v-model="addOrUpdateData.title" autocomplete="off" placeholder="请输入标题"></el-input>
				</el-form-item>
				<el-form-item label="简介">
					<el-input v-model="addOrUpdateData.introduct" placeholder="请输入简介"></el-input>
				</el-form-item>
				<el-form-item label="封面图">
					<el-upload action="http://localhost:8080/sfzy/fileUpload" list-type="picture-card"
						:on-success="handleSuccess" :limit="1" :on-preview="handlePictureCardPreview"
						:file-list="fileList" :on-remove="handleRemove" aria-required="">
						<i class="el-icon-plus"></i>
					</el-upload>
					<!-- 放大预览组件: 和官网一致 -->
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>
				<el-form-item label="咨询师">
					<el-select v-model="addOrUpdateData.consultantsId" placeholder="咨询师">
						<el-option v-for="item in options" :key="item.id" :label="item.consultantName" :value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="内容">
					<quill-editor v-model="addOrUpdateData.context"></quill-editor>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="confirm">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				// 查询条件对象，如果异步请求数据，需要携带这些参数
				queryData: {
					title: '',
					introduct: '',
					context: '',
					pageSize: 10, // 每页显示的条目数
					pageNum: 1 // 页码
				},
				options: [],
				total: 0, // 总条数
				dataList: [], // 当前页列表数据
				title: '新增', // 动态标题，
				dialogFormVisible: false, // 控制对话框是否显示，默认值false
				// 新增或修改数据对象
				addOrUpdateData: {
					consultantsId: 20,
					title: '',
					introduct: "",
					context: "",
					creatTime: "",
				},
				ids: [], // 删除元素的id集合
				dialogImageUrl: '', // 图片预览框的地址
				dialogVisible: false, // 控制图片预览框是否显示
				fileList: []
			}
		},
		methods: {
			handleSelectionChange(val) {
				this.ids = val.map(function(item) {
					return item.id;
				});
			},
			removeBatch() {
				if (this.ids.length == 0) {
					return;
				}
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					// 发送异步请求删除数据
					this.$ajax.delete('sysPsyKnowledge?ids=' + this.ids).then(resp => {
						if (resp.code == 200) {
							this.$message({
								type: 'success',
								message: '删除成功!'
							});
							this.queryList()
						} else {
							this.$message({
								type: 'error',
								message: '删除失败!'
							});
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			// 上传成功的回调，res服务器响应的数据
			handleSuccess(res, file) {
				this.addOrUpdateData.chart = res.data; // res.data就是图片上传的地址
			},
			// 处理删除图片
			handleRemove(file, fileList) {
				this.addOrUpdateData.chart = ''
			},
			// 处理图片预览
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},
			// 删除
			remove(id) {
				// 提示
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					// 发送异步请求删除数据
					this.$ajax.delete('sysPsyKnowledge/' + id).then(resp => {
						if (resp.code == 200) {
							this.$message({
								type: 'success',
								message: '删除成功!'
							});
							this.queryList()
						} else {
							this.$message({
								type: 'error',
								message: '删除失败!'
							});
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			// 新增
			add() {
				this.title = '新增'
				this.dialogFormVisible = true
			},
			edit(row) { // row就是当前行的数据
				this.title = '编辑'
				this.dialogFormVisible = true
				//根据id实时查询数据， 然后进行回显
				this.$ajax.get('sysPsyKnowledge/' + row.id).then(resp => {
					this.addOrUpdateData = resp.data
					var chart = this.addOrUpdateData.chart; // /upload/20240712 / xdasd.jpg
					this.fileList.push({
						name: chart.substr(chart.lastIndexOf('\\') + 1),
						// xdasd.jpg
						url: 'http://localhost:8080/sfzy' + chart
					})
				})
			},
			// 取消
			cancle() {
				// 重置表单数据
				this.addOrUpdateData = {
					consultantsId: "",
					title: '',
					introduct: "",
					context: "",
					creatTime: "",
					consultantName: ""

				}
				// 重置图片展示列表
				this.fileList = []
				// 关闭对话框
				this.dialogFormVisible = false
			},
			// 提交数据
			confirm() {
				if (this.addOrUpdateData.id) { // 执行修改
					this.$ajax.put('sysPsyKnowledge', this.addOrUpdateData)
						.then((resp) => {
							if (resp.code == 200) {
								// 提示
								this.$message.success("修改成功!")
								// 关闭对话框
								this.dialogFormVisible = false
								// 再次查询
								this.queryList()
							} else {
								this.$message.error("修改失败!")
							}
						})
				} else { // 执行新增
					this.$ajax.post('sysPsyKnowledge', this.addOrUpdateData)
						.then((resp) => {
							if (resp.code == 200) {
								// 提示
								this.$message.success("新增成功!")
								// 关闭对话框
								this.dialogFormVisible = false
								// 再次查询
								this.queryList()
							} else {
								this.$message.error("新增失败!")
							}
						})
				}
			},
			// pageSize 改变时会触发
			handleSizeChange(size) {
				// 把最新的每页条目数赋值给查询条件对象
				this.queryData.pageSize = size
				// 每页条目数发生改变，页码设置为1
				this.queryData.pageNum = 1
				// 查询数据
				this.queryList()
			},
			// pageNum 改变时触发
			handleCurrentChange(num) {
				this.queryData.pageNum = num
				// 查询数据
				this.queryList()
			},
			// 封装获取文章列表数据的方法
			queryList() {
				this.$ajax.get('sysPsyKnowledge/page', {
					params: this.queryData
				}).then((resp) => {
					if (resp.code == 200) {
						this.dataList = resp.data.list
						this.total = resp.data.total
					} else {
						this.$message.error('数据获取异常!')
					}
				})
				this.$ajax.get('sysConsultants', {
					params: this.queryData
				}).then((resp) => {
					if (resp.code == 200) {
						this.options = resp.data
					} else {
						this.$message.error('数据获取异常!')
					}
					console.log(this.options);
				})
			}
		},
		mounted() {
			this.queryList()
		}
	}
</script>
<style scoped>
	.el-breadcrumb {
		border-bottom: 1px solid #ddd;
		padding-bottom: 15px;
	}

	.el-form {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 6px;
	}

	.el-pagination {
		margin-top: 15px;
	}

	/deep/ .ql-container {
		min-height: 200px;
	}

	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px !important;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>